package com.yanqu.road.server.cmd.cricket.court;

import com.yanqu.road.entity.cricket.CricketMatchData;
import com.yanqu.road.entity.cricket.UserCricketData;
import com.yanqu.road.entity.cricket.court.CricketCourtBattleData;
import com.yanqu.road.entity.cricket.court.CricketCourtMatchData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketBattle;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketBattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketMatchUserData;
import com.yanqu.road.server.pb.CricketPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;


@Cmd(code = GameProtocol.S_CRICKET_BATTLE_START, desc = "蛐蛐战斗")
public class CricketBattleStartCmd extends AbstractCommand {


    private long targetUserId;

    private List<Integer> cricketIdList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CricketBattleStartReqMsg reqMsg = CricketProto.CricketBattleStartReqMsg.parseFrom(packet.getBytes());
        targetUserId = reqMsg.getUserId();
        cricketIdList = reqMsg.getCricketIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CricketModule cricketModule = player.getModule(CricketModule.class);
        UserCricketData userCricketData = cricketModule.getUserCricketData();
        CricketMatchData cricketMatchData = userCricketData.getCricketMatchData();
        if (cricketMatchData == null) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        if (targetUserId == 0) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }
        // 匹配信息
        CricketCourtMatchData targetMatchDefenseData = cricketMatchData.getTargetDefenseData();

        if (targetMatchDefenseData == null) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        int round = targetMatchDefenseData.getRound();
        int courtId = targetMatchDefenseData.getCourtId();
        int groupId = targetMatchDefenseData.getGroupId();

        CricketProto.CricketUserBattleMatchDataTemp cricketUserBattleMatchDataTemp = null;
        for (CricketCourtBattleData data : targetMatchDefenseData.getBattleDataList()) {
            if (data.getUserId() == targetUserId) {
                cricketUserBattleMatchDataTemp = CricketPb.parseCricketUserBattleMatchDataTemp(data).build();
                break;
            }
        }

        if (cricketUserBattleMatchDataTemp == null || cricketUserBattleMatchDataTemp.getDefenseList().size() == 0) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        ret = cricketModule.preBattle(cricketIdList);
        if (ret != 0) {
            return;
        }

        CricketMatchUserData cricketMatchUserData = CricketMatchUserData.getInstance();
        cricketMatchUserData.setUserId(player.getUserId());
        cricketMatchUserData.setUserBattleData(cricketModule.toCricketUserBattleDataTemp().build());
        for (int cricketId : cricketIdList) {
            CricketBattleData cricketBattleData = cricketModule.toCricketBattleData(cricketId);
            if (cricketBattleData == null) {
                ret = GameErrorCode.E_CRICKET_CRICKET_NOT_FOUND;
                return;
            }
            cricketMatchUserData.getCricketDataList().add(cricketBattleData);
        }

        CricketMatchUserData cricketMatchUserData1 = CricketMatchUserData.getInstance();
        cricketMatchUserData1.setUserId(cricketUserBattleMatchDataTemp.getUserInfo().getUserId());
        cricketMatchUserData1.setUserBattleData(cricketUserBattleMatchDataTemp.getUserInfo());
        for (CricketProto.CricketMatchDataTemp cricketMatchDataTemp : cricketUserBattleMatchDataTemp.getDefenseList()) {
            CricketBattleData cricketBattleData = CricketBattleData.getInstance(cricketMatchDataTemp);
            cricketMatchUserData1.getCricketDataList().add(cricketBattleData);
        }

        CricketBattle cricketBattle = CricketBattle.getInstance(cricketMatchUserData, cricketMatchUserData1);
        cricketModule.handleGameBattleLog(cricketBattle.battle(), cricketUserBattleMatchDataTemp, round, courtId, groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            CricketProto.CricketBattleStartRespMsg.Builder respMsg = CricketProto.CricketBattleStartRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_CRICKET_BATTLE_START, respMsg);
        }

    }
}
